package xhwl.hmo.mall.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

import xhwl.hmo.mall.R;
import xhwl.hmo.mall.http.bean.resp.PersonInfo;
import xhwl.hmo.mall.ui.CertificateActivity;
import xhwl.hmo.mall.views.MyListView;
import xhwl.pulltorefresh.adapter.BaseAdapter;
import xhwl.pulltorefresh.adapter.ViewHolder;

public class PersonListAdapter extends BaseAdapter<PersonInfo> {
    List<PersonInfo> mList;
    OnItemClickListener onItemClickListener;
    Handler handler=new Handler();
    int extendPosition=-1;//当前展开的列表项
    public PersonListAdapter(Context context, List<PersonInfo> list) {
        super(context, R.layout.person_list_item, list);
        this.mList = list;
    }
    public void setOnItemClickListener(OnItemClickListener onItemClickListener){
        this.onItemClickListener=onItemClickListener;
    }
    @Override
    public void convert(final ViewHolder holder, final PersonInfo resp) {
        holder.setText(R.id.tvPersonName, resp.PERSON_NAME);
        holder.setText(R.id.tvServeCount, "共保障"+resp.TYPE_NUM +"项服务");
        final int pos = holder.getLayoutPosition();
        final MyListView listView = (MyListView) holder.getView(R.id.listView);
        PersonListItemAdapter adapter=new PersonListItemAdapter(context,resp.BZ_LIST);
        listView.setAdapter(adapter);
        final ImageView ivPointer=holder.getView(R.id.ivPointer);
        if(extendPosition==pos){
            ivPointer.setImageResource(R.drawable.person_ic_down);
            listView.setVisibility(View.VISIBLE);
        }
        else
        {
            ivPointer.setImageResource(R.drawable.person_icon_right);
            listView.setVisibility(View.GONE);
        }
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(extendPosition==pos){
                    extendPosition=-1;
                }
                else{
                    extendPosition=pos;
                }
                notifyDataSetChanged();
            }
        });
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                PersonInfo.Serve data = resp.BZ_LIST.get(i);
                String fromFlag=data.FROM_FLAG;
//                if("7".equals(fromFlag)||"9".equals(fromFlag)||"10".equals(fromFlag)){
//                    if("已生效".equals(data.BX_STATUS)||"观察期".equals(data.BX_STATUS)){
                        Intent intent=new Intent(context,CertificateActivity.class);
                        intent.putExtra(CertificateActivity.EXTRA_BUT_ID,data.BUY_ID);
                        intent.putExtra(CertificateActivity.EXTRA_APPLY_CARD_FLAG,true);
                        intent.putExtra(CertificateActivity.EXTRA_PERSON_ID,data.PERSON_ID);
                        intent.putExtra(CertificateActivity.EXTRA_SX_ID,data.SX_ID);
                        intent.putExtra(CertificateActivity.EXTRA_SALE_NAME,data.SALE_NAME);
                        intent.putExtra(CertificateActivity.EXTRA_GOODS_ID,data.GOODS_ID);
                        intent.putExtra(CertificateActivity.EXTRA_APPLY_CARD_FLAG,"7".equals(fromFlag)||"9".equals(fromFlag)||"10".equals(fromFlag));
                        context.startActivity(intent);
//                    }
//                }

            }
        });
    }
    public interface OnItemClickListener{
        void onDelete(int position);
        void onEdit(int position);
        void onItemClick(int position);
    }
    class PersonListItemAdapter extends android.widget.BaseAdapter{
        List<PersonInfo.Serve> list;
        Context context;
        public PersonListItemAdapter(Context context,List<PersonInfo.Serve> list){
            this.list=list;
            this.context=context;
        }
        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int i) {
            return list.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            PersonInfo.Serve serve=list.get(i);
            view= LayoutInflater.from(context).inflate(R.layout.person_list_serve_item,null);
            TextView tvName= (TextView) view.findViewById(R.id.tvServeName);
            TextView tvStatus= (TextView) view.findViewById(R.id.tvServeStatus);
            tvName.setText(serve.SALE_NAME);
            tvStatus.setText(serve.BX_STATUS);
            if("已失效".equals(serve.BX_STATUS)){
                tvStatus.setTextColor(Color.parseColor("#999999"));
            }else if("观察期".equals(serve.BX_STATUS)){
                tvStatus.setTextColor(Color.parseColor("#ffa21b"));
            }else if("已生效".equals(serve.BX_STATUS)){
                tvStatus.setTextColor(Color.parseColor("#32a7df"));
            }
            return view;
        }
    }
}
